package com.lb.bs.client.processor.handle.dynamics.remove;

import com.lb.bs.client.annotation.SingletonMark;
import com.lb.bs.client.beans.LBGrayIPBean;
import com.lb.bs.client.beans.LBGrayItemBean;
import com.lb.bs.client.repository.StoreCenter;
import com.lb.bs.client.processor.ProcessorHandleService;
import com.lb.bs.client.processor.beans.ProcessorDynamicHandlerBean;
import com.lb.bs.client.util.GrayUtil;
import com.lb.bs.client.util.KeyGenerateUtil;

import java.util.Collections;
import java.util.Map;

/**
 * program: LB-BS-Client
 * author: bsworld.xie
 * create: 2019-12-18 20:37
 * description:
 */
@SingletonMark
public class GrayRemoveProcessorHandle implements ProcessorHandleService<ProcessorDynamicHandlerBean> {
    @Override
    public void handleProcess(ProcessorDynamicHandlerBean handlerBean) {
        //是灰度，走灰度策略
        String zkKey = handlerBean.getKey();
        boolean isGrayIpKey = GrayUtil.isGrayIpKey(zkKey);

        if (isGrayIpKey) {
            Map<String, LBGrayIPBean> grayMap = StoreCenter.getInstance().getGrayIPKeyMap();
            LBGrayIPBean ipBean = grayMap.get(zkKey);
            if (ipBean != null) {
                ipBean.setGrayIpList(Collections.emptyList());
            }
        }
        if (GrayUtil.isGrayItemKey(zkKey)) {
            Map<String, LBGrayItemBean> grayItemKeyMap = StoreCenter.getInstance().getGrayItemKeyMap();
            String grayItemKey = KeyGenerateUtil.getItemKeyByGrayItemKey(zkKey);
            LBGrayItemBean grayItemBean = grayItemKeyMap.get(grayItemKey);
            if (grayItemBean != null) {
                grayItemBean.setGrayValue(null);
            }
        }

    }
}
